package com.zhuanyongjin.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.zhuanyongjin.dao.member_yongjinDao;
import com.zhuanyongjin.util.DatabaseAccess;
import com.zhuanyongjin.util.EasyMapsManager;
import com.zhuanyongjin.util.MD5;
import com.zhuanyongjin.util.Calenda;

public class member_yongjinDaoImpl implements member_yongjinDao {

	@SuppressWarnings({ "rawtypes", "static-access" })
	   public List querymerber_yongjin(int Id,String member_id,int yongjin_id,int rows,int page){
		// TODO Auto-generated method stub
		String sql = "select qb_members.uid from member_yongjing where member_yongjin.username=? and member_yongjin.password=?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(Id);
			easyMapsManager.setPreparedParameter(member_id);
			easyMapsManager.setPreparedParameter(yongjin_id);
			easyMapsManager.setPreparedParameter((page - 1) * rows);
			easyMapsManager.setPreparedParameter(rows);
			List list = easyMapsManager.executeQuery(sql);
			databaseAccess.close();
			return list;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean updatemember_yongjin(int member_id,int yongjin_id){
		boolean b=false;
		DatabaseAccess dao=new DatabaseAccess();
		Calenda C=new Calenda();
		String now=C.getNowTime().toString();
		String sql="update member_yongjin set is_sure=1,end_time=? where  is_delete=0 AND member_id=? AND yongjin_id=?;";
		dao.setPreparedParameter(now);
		dao.setPreparedParameter(member_id);
		dao.setPreparedParameter(yongjin_id);
		boolean b1=dao.executeUpdate(sql);
		DatabaseAccess dao1=new DatabaseAccess();
		String sql2="update member_yongjin set is_sure=2 where is_delete=0 AND yongjin_id=? AND member_id<>?;";
		dao1.setPreparedParameter(yongjin_id);
		dao1.setPreparedParameter(member_id);
		boolean b2=dao1.executeUpdate(sql2);
		if(b1==true&&b2==true){
			b=true;
		}
		return b;
	}
	
	public List querycomplete(int rows,int page){
		String sql ="select * from member_yongjin j left join members m on j.member_id=m.Id where " +
				"j.is_complete=1 LIMIT ?,?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter((page - 1) * rows);
			easyMapsManager.setPreparedParameter(rows);
			List list = easyMapsManager.executeQuery(sql);
			databaseAccess.close();
			return list;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean delectmember_yongjin(int Id){
		DatabaseAccess dao=new DatabaseAccess();
		String sql="update yongjin set is_delete=1 where Id=?;";
		dao.setPreparedParameter(Id);
		boolean b=dao.executeUpdate(sql);
		return b;
	}
	
	public List jiedanmerber_yongjin(String member_id, int rows, int page){
		String sql ="select * from member_yongjin m left join yongjin y on m.yongjin_id=y.Id where m.member_id=? LIMIT ?,?;";
               try{
	                   DatabaseAccess databaseAccess = new DatabaseAccess(false);
	                   EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
	                   easyMapsManager.setPreparedParameter(member_id);
	                   easyMapsManager.setPreparedParameter((page - 1) * rows);
	                   easyMapsManager.setPreparedParameter(rows);
	                   List list = easyMapsManager.executeQuery(sql);
	                   databaseAccess.close();
	                   return list;
                     }catch(Exception e) {
	                       e.printStackTrace();
                                           }
                          return null;
	}
	
	
	public boolean getyongjin(int member_id, int yongjin_id){
		DatabaseAccess dao=new DatabaseAccess();
		Date now=new Date();
		String sql="insert into member_yongjin(member_id,yongjin_id,is_complete,start_time,is_delete,is_pay,is_sure) values(?,?,?,?,?,?,?);";
		dao.setPreparedParameter(member_id);
		dao.setPreparedParameter(yongjin_id);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(now);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(0);
		dao.setPreparedParameter(0);
		boolean b=dao.executeInsert(sql)>0?true:false;
		return b;
	}
	public boolean paymember_yongjin(int id){
		DatabaseAccess dao=new DatabaseAccess();
		Date now=new Date();
		String sql="update member_yongjin set is_pay=?,pay_time=? where Id=?";
		dao.setPreparedParameter(1);
		dao.setPreparedParameter(now);
		dao.setPreparedParameter(id);
		boolean b=dao.executeInsert(sql)>0?true:false;
		return b;
	}
	
	public boolean qiangdan(int member_id, int yongjin_id){
		DatabaseAccess dao=new DatabaseAccess();
		String sql="update member_yongjin set is_sure=1 where member_id=? AND yongjin_id=?;";
		dao.setPreparedParameter(member_id);
		dao.setPreparedParameter(yongjin_id);
		boolean b=dao.executeUpdate(sql);
		return b;
	}
	
	public List queryexistmembers(int member_id, int yongjin_id){
		String sql ="select * from member_yongjin m where m.member_id=? AND m.yongjin_id=?";
        try{
                DatabaseAccess databaseAccess = new DatabaseAccess(false);
                EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
                easyMapsManager.setPreparedParameter(member_id);
                easyMapsManager.setPreparedParameter(yongjin_id);
                List list = easyMapsManager.executeQuery(sql);
                databaseAccess.close();
                return list;
              }catch(Exception e) {
                    e.printStackTrace();
                                    }
                   return null;
	}
	
	public boolean receive_delect_my(int member_id, int yongjin_id){
		DatabaseAccess dao=new DatabaseAccess();
		String sql="update member_yongjin set is_delete=1 where member_id=? AND yongjin_id=?;";
		dao.setPreparedParameter(member_id);
		dao.setPreparedParameter(yongjin_id);
		boolean b=dao.executeUpdate(sql);
		return b;
	}
}
